// pages/jilu/jilu.js

var api = require('../../js/api.js');
var util = require('../../utils/util.js');
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        current: 1,


        vouis: [],

        urrs: [{
            resultAcr: "0.00",
            resultBil: "阴性(-)",
            resultBld: "阴性(-)",
            resultCa: "0",
            resultCre: "0.9",
            resultGlu: "阳性(+1)",
            resultKet: "阴性(-)",
            resultLeu: "阴性(-)",
            resultMa: "0",
            resultNit: "阴性(-)",
            resultPh: "5.0",
            resultPro: "阴性(-)",
            resultSg: "1.030",
            resultUbg: "阴性(-)",
            resultVc: "阳性(+1)",
            inspectTime: "2020-11-13 15:21:00"
        }]

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        api.checkToken().then(b => {
            if (b) {
                that.findUi(that.data.current)
            }
        })


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

        let that = this
        that.setData({
            current: 1
        })
        that.findUi(that.data.current)


        //当逻辑执行完后关闭刷新
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    nextPage: function () {

        app.log('==>>翻页')
        var that = this
        let _current = that.data.current + 1
        that.setData({
            current: _current
        })
        app.log(_current)

        that.findUi(that.data.current)
    },
    findUi: function (current) {

        var that = this
        that.get_jilu(current)

    },
    toExplain: function (e) {

        let index = e.currentTarget.dataset.index
        var that = this
        wx.navigateTo({
            url: '/pages/pingu/pingu?voui=' + JSON.stringify(that.data.vouis[index])
        })
    },
    toShows: function (e) {
        let index = e.currentTarget.dataset.index
        var that = this
        wx.navigateTo({
            url: '/pages/jilu/shows/shows?voui=' + JSON.stringify(that.data.vouis[index])
        })
    },
    get_jilu: function (current) {
        var that = this

        let data = {
            "parameters": {
                "userId": app.globalData.users[app.globalData.usersIndex].userId,
            },
            "current": current,
            "size": 10
        }

        api.findUiByPager(JSON.stringify(data)).then(res => {
            app.log('==>>res_data.records:')
            app.log(res.data.data.records)

            app.log('====>>that.data.vouis current')
            app.log(that.data.vouis)
            app.log(that.data.current)


            that.data_handle(res.data.data.records)


        })
    },
    upd_top_user: function (e) {

        var that = this

        that.setData({
            current: e.detail
        })
        that.get_jilu(that.data.current)
    },

    set_open_ret: function (e) {

        let that = this
        let index = e.currentTarget.dataset.index

        app.log('==>>展开:' + index)

        let uis = that.data.vouis

        for (let i in uis) {
            if (i == index) {
                uis[i].openRet = !uis[i].openRet
            } else {
                uis[i].openRet = false
            }
        }

        that.setData({
            vouis: uis
        })


    },
    data_handle: function (uis) {
        var that = this

        app.log('==>>data_format')
        app.log(uis)
        //
        // for (let i in uis) {
        //
        //     let time = uis[i].ur.inspectTime
        //     util.formatDateTime(new Date(time), 'Y年M月D日').then(res => {
        //         uis[i].ur.f_year = res
        //     })
        //     util.formatDateTime(new Date(time), 'h:m').then(res => {
        //         uis[i].ur.f_hour = res
        //     })
        // }
        //
        // setTimeout(function () {
        //     if (that.data.current == 1) {
        //         that.setData({
        //             vouis: []
        //         })
        //     }
        //     that.setData({
        //         vouis: that.data.vouis.concat(uis)
        //     })
        // }, 1000)
        //


        if (that.data.current == 1) {
            that.setData({
                vouis: uis
            })
        } else {
            that.setData({
                vouis: that.data.vouis.concat(uis)
            })
        }


    }

})